﻿using System;
using System.Data;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MDSAdministration
{
    public class ConverterHelper
    {
        public static object Convert(object[] values, Type targetType, object parameter, CultureInfo culture, DependencyProperty dp)
        {
            if (values[1] is DataRow)
            {
                var cell = (DataGridCell)values[0];
                cell.Background = null;
                cell.ToolTip = null;

                var row = (DataRow)values[1];

                string columnName = cell.Column.Header.ToString();

                if (columnName == Constants.MDSADMINISTRATION_CODE &&
                    row[Constants.EXECELUPLOAD_CODE_DUP] != DBNull.Value &&
                    (bool)row[Constants.EXECELUPLOAD_CODE_DUP] == true)
                {   
                    if (dp == DataGridCell.BackgroundProperty)
                    {
                        cell.Background = Brushes.LightSeaGreen;
                    }
                    else if (dp == DataGridCell.ToolTipProperty)
                    {
                        cell.ToolTip = "Duplicate Code value.";
                    }
                    //Not sure why below doesn't work
                    //return new SolidColorBrush(Colors.LightSalmon);
                }
                else if (row.Table.Columns.Contains(Constants.EXCELUPLOAD_MDSADMIN + columnName + "_Missing"))
                {
                    if (row[Constants.EXCELUPLOAD_MDSADMIN + columnName + "_InActive"] != DBNull.Value &&
                        (bool)row[Constants.EXCELUPLOAD_MDSADMIN + columnName + "_InActive"] == true)
                    {   
                        if (dp == DataGridCell.BackgroundProperty)
                        {
                            cell.Background = Brushes.LightSalmon;
                        }
                        else if (dp == DataGridCell.ToolTipProperty)
                        {
                            cell.ToolTip = "This value is inactive.";
                        }
                    }
                    else if (row[Constants.EXCELUPLOAD_MDSADMIN + columnName + "_Missing"] != DBNull.Value &&
                             (bool)row[Constants.EXCELUPLOAD_MDSADMIN + columnName + "_Missing"] == true)
                    {
                        if (dp == DataGridCell.BackgroundProperty)
                        {
                            cell.Background = Brushes.Plum;    
                        }
                        else if (dp == DataGridCell.ToolTipProperty)
                        {
                            cell.ToolTip = "This value is missing.";
                        }
                    }
                    else
                    {
                        if (dp == DataGridCell.BackgroundProperty)
                        {
                            cell.ClearValue(DataGridCell.BackgroundProperty);
                        }
                        else if (dp == DataGridCell.ToolTipProperty)
                        {
                            cell.ClearValue(DataGridCell.ToolTipProperty);
                        }
                    }
                }
                else
                {
                    if (dp == DataGridCell.BackgroundProperty)
                    {
                        cell.ClearValue(DataGridCell.BackgroundProperty);
                    }
                    else if (dp == DataGridCell.ToolTipProperty)
                    {
                        cell.ClearValue(DataGridCell.ToolTipProperty);
                    } 
                }
            }
            return null;
        }
    }
}